package pl.com.ozzy.ozsign.tools;

import pl.com.ozzy.ozsign.lang.IUIDrawPanelCallback;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.util.Log;
import android.view.MotionEvent;

public class SignaturesProcessor implements GestureOverlayView.OnGestureListener {
	
	private final static String THIS_FILE = "GestureProcessor";
	private static final float LENGTH_THRESHOLD = 120.0f;
	
	private Gesture mGesture;
	private IUIDrawPanelCallback mDelegate;
	
    /**
     * Constructor for GestureProcessor 
     * @param gesture gesture object
     * @param delegate object which can make UI specific operations
     */
	public SignaturesProcessor(Gesture gesture, IUIDrawPanelCallback delegate) {
		Log.d(THIS_FILE, "Creating new gesture processor");
		mGesture = gesture;
		mDelegate = delegate;
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.gesture.GestureOverlayView.OnGestureListener#onGesture(android.gesture.GestureOverlayView, android.view.MotionEvent)
	 */
	@Override
	public void onGesture(GestureOverlayView overlay, MotionEvent event) {
	}

	/*
	 * (non-Javadoc)
	 * @see android.gesture.GestureOverlayView.OnGestureListener#onGestureCancelled(android.gesture.GestureOverlayView, android.view.MotionEvent)
	 */
	@Override
	public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
	}

	/*
	 * (non-Javadoc)
	 * @see android.gesture.GestureOverlayView.OnGestureListener#onGestureEnded(android.gesture.GestureOverlayView, android.view.MotionEvent)
	 */
	@Override
	public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
		mGesture = overlay.getGesture();
        if (mGesture.getLength() < LENGTH_THRESHOLD) {
            overlay.clear(false);
        } else {
        	Log.d(THIS_FILE, "Gesture length == "+mGesture.getLength());
        }
        mDelegate.executeOnEnded();
        Log.d(THIS_FILE, "Gesture ended");
	}

	/*
	 * (non-Javadoc)
	 * @see android.gesture.GestureOverlayView.OnGestureListener#onGestureStarted(android.gesture.GestureOverlayView, android.view.MotionEvent)
	 */
	@Override
	public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
		Log.d(THIS_FILE, "Gesture started");
        mGesture = null;
        mDelegate.executeOnStarted();
	}
	
	/**
	 * Returns gesture created with GestureProcessor
	 * @return gesture
	 */
	public Gesture getGesture() {
		return mGesture;
	}

	
}
